#!/bin/bash
#-----------rpms.tar.gz的制做-------
# 随便创建一个指定的目录,并cd到指定的目录中
# yum install epel-release -y
# yum install ansible --downloadonly  --downloaddir=./
# yum install httpd sshpass nginx createrepo --downloadonly --downloaddir=./
# createrepo ./
# tar zcvf rpms.tar.gz ./
#----------------------------------

#获取当前的目录
CUR_PATH=$(readlink -f $(dirname $0))
if [ ! -e $CUR_PATH/config.ini ]; then
    echo "Config File config.ini is not exists..please check..."
    exit 1
fi
if [ -e $CUR_PATH/pre_check.log ]; then
    rm -f $CUR_PATH/pre_check.log
fi

MANAGE_HOST_IP=$(cat $CUR_PATH/config.ini | sed -n '/\[hosts\]/,/\[.*\]/p' | grep -v '\[.*\]' | grep -v \^\# | grep -v \^[[:space:]]\*\$ | awk '$3=="true"{print $1}')
RPM_PKGS_DIR="/opt/rpms"
RPM_PKGS_NAME="rpms.tar.gz"
HOSTS=$(cat $CUR_PATH/config.ini | sed -n '/\[hosts\]/,/\[.*\]/p' | grep -v '\[.*\]' | grep -v \^\# | grep -v \^[[:space:]]\*\$ | awk '{print $1}')

if [ ! -e $CUR_PATH/$RPM_PKGS_NAME ]; then
    echo "Packages $RPM_PKGS_NAME is not found in current dir...Please check..."
    exit 1
fi

if [ ! -d $RPM_PKGS_DIR ]; then
    mkdir -p $RPM_PKGS_DIR
fi

function result_echo() {
    if [ $? -eq 0 ]; then
        echo "$1" | tee -a >>$CUR_PATH/pre_check.log
    else
        echo "$2" | tee -a >>$CUR_PATH/pre_check.log
        exit 1
    fi
}

tar zxvf $CUR_PATH/$RPM_PKGS_NAME -C $RPM_PKGS_DIR

# 备份CentOS系统默认的repo文件
cd /etc/yum.repos.d/
if [ ! -d "backup" ]; then
    mkdir backup
    mv *.repo ./backup
fi

# 创建本地的local.repo文件,并将baseurl指向/opt/rpms

cat <<EOF >/etc/yum.repos.d/local.repo
[Local]
name=Local
baseurl=file:///opt/rpms/
enabled=1
gpgcheck=0

EOF

#查看是否有local.repo的源
yum repolist | grep ^Local &>/dev/null
result_echo "OK...Config Local YUM Success..." "Sorry,Config Local YUM Failed...Pleas Check..."

# 更改apache httpd服务默认监听端口号,需要关闭selinux
setenforce 0

# 创建基于HTTP服务的YUM源
yum install httpd -y
#修改端口号
sed -i '/^Listen /d' /etc/httpd/conf/httpd.conf
sed -i '$aListen 9090' /etc/httpd/conf/httpd.conf
sleep 3
#启动httpd的服务
systemctl restart httpd
ss -tnl | grep :9090 &>/dev/null
result_echo "OK...Web Server HTTPD Start Success..." "Sorry,Web Server HTTPD Start Failed...Please Check..."

ln -sv /opt/rpms/ /var/www/html/rpms
cat <<EOF >/etc/yum.repos.d/http.repo
[HTTP]
name=HTTP
baseurl=http://$MANAGE_HOST_IP:9090/rpms
gpgcheck=0
enabled=1
EOF

yum repolist | grep ^HTTP &>/dev/null
result_echo "OK...Config HTTP YUM Success..." "Sorry,Config HTTP YUM Failed...Pleas Check..."

#管理节点基础软件安装,包括sshpass,ansible
yum install sshpass ansible -y
#管理节点生成密钥对,利用sshpass将公钥信息传输到所有节点,实现管理节点对所有节点的免密登录

echo "y\n" | ssh-keygen -t rsa -q -N "" -f ~/.ssh/id_rsa
for host in $HOSTS; do
    sshpass -proot ssh-copy-id -o StrictHostKeyChecking=no root@$host &>/dev/null
    ssh -o StrictHostKeyChecking=no root@$host "df -h" &>/dev/null
    result_echo "OK...Use Nopassword Login To $host Success..." "Sorry,Use Nopassword Login To $host Success...Pleas Check..."
done

# 配置ansible使用的hosts
if [ -e $CUR_PATH/hosts ]; then
    rm -f $CUR_PATH/hosts
fi
for host in $HOSTS; do
    echo $host >>$CUR_PATH/hosts
done

# 操作系统初始化,关闭firewalld,关闭slinux,关闭内存swap
ansible -i $CUR_PATH/hosts all -m shell -a "systemctl stop firewalld"
result_echo "OK...Stop Firewalld Success..." "Sorry,Stop Firewalld Failed...Please Check..."
ansible -i $CUR_PATH/hosts all -m shell -a "systemctl disable firewalld"
result_echo "OK...Disable Firewalld Auto Startup Success" "Sorry,Disable Firewalld Auto Startup Failed...Please Check..."
ansible -i $CUR_PATH/hosts all -m shell -a "getenforce | grep Disabled &>/dev/null || setenforce 0"
result_echo "OK...Stop Selinux Success" "Sorry,Stop Selinux Failed...Please Check..."
ansible -i $CUR_PATH/hosts all -m lineinfile -a 'path=/etc/sysconfig/selinux regexp="^SELINUX=" line="SELINUX=disabled"'
result_echo "OK...Disable Selinux Auto Startup Success" "Sorry.Disable Selinux Auto Startup Failed...Please Check..."

ansible -i $CUR_PATH/hosts all -m shell -a "swapoff -a"
result_echo "OK...Close Memory Swap Success" "Sorry,Close Memory Swap Failed...Please Check..."
ansible -i $CUR_PATH/hosts all -m lineinfile -a 'path=/etc/fstab regexp="swap" state=absent'
result_echo "OK...Close Memory Swap Auto Startup Success" "Sorry,Close Memory Swap Auto Startup Failed...Please Check..."

cat $CUR_PATH/config.ini | sed -n '/\[hosts\]/,/\[.*\]/p' | grep -v '\[.*\]' | grep -v \^\# | grep -v \^[[:space:]]\*\$ >$CUR_PATH/tempHost.info
cat $CUR_PATH/tempHost.info | while read line; do
    IP_ADDRESS=$(echo $line | awk '{print $1}')
    HOST_NAME=$(echo $line | awk '{print $2}')

    ansible -i $CUR_PATH/hosts $IP_ADDRESS -m shell -a "hostnamectl set-hostname $HOST_NAME"
    result_echo "OK...Set Hostname $HOST_NAME ON $IP_ADDRESS Success" "Sorry,Set Hostname $HOST_NAME ON $IP_ADDRESS Failed...Please Check..."

    ansible -i $CUR_PATH/hosts all -m lineinfile -a "path=/etc/hosts line='$IP_ADDRESS  $HOST_NAME'"
    result_echo "OK...Add $IP_ADDRESS $HOST_NAME in /etc/hosts Success" "Sorry,Add $IP_ADDRESS  $HOST_NAME in /etc/hosts Failed...Please Check..."
done
